<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\ProjectInfoModel;
use share\service\cache\CommonCacheKeyService;
use share\exception\ApiException;
use share\service\BaseModelService;
use share\service\NotifyDingTalkService;
use share\service\RedisService;
use share\utils\Helper;

class ProjectInfoService extends BaseModelService
{
    public static function getModel()
    {
        return new ProjectInfoModel;
    }

    // const CACHE_KEY_PREFIX_PROJECT_INFO = 'aliyun:project';

    public static function getByVendor($vendor)
    {
        $vendor = self::getRelationVendor($vendor);
        $datas = self::fastGetAll();
        return Helper::filterOne($datas, ["vendor" => $vendor]);
    }

    /**
     * 如果存在多个vendor对应一个projectId的情况   在此处转换
     */
    public static function getRelationVendor($vendor = '')
    {
        $vendorKey = strtoupper($vendor);
        $relation = [
            'FSAN' => 'FSLINK',
            'KNOWLEDGE' => 'HDT-KNOWLEDGE',
            'HDT-LECOO' =>  'HDT-LENOVO',
            'HDT-NEUTRALEN' => 'ENZ',
            'HDT-NEUTRAL' => 'ENZ',
        ];
        if (isset($relation[$vendorKey])) {
            return $relation[$vendorKey];
        }
        return $vendor;
    }

    public static function getCacheTimeout()
    {
        $timeout = config("user_cache_expire");
        if (empty($timeout)) {
            $timeout = 7200; //两小时
        }
        return $timeout;
    }

    public static function getCacheKey()
    {
        return CommonCacheKeyService::getCacheKey( CommonCacheKeyService::CACHE_KEY_ALIYUN_PROJECT_INFO );
    }

    public static function fastGetAll()
    {
        $timeout = self::getCacheTimeout();
        $key = self::getCacheKey();
        $datas = RedisService::instance(RedisService::CACHE_DB_BASE)->getJson($key);
        if (empty($datas)) {
            $datas =  self::getAllByFilter([]);
            if (empty($datas)) {
                return null;
            }
            RedisService::instance(RedisService::CACHE_DB_BASE)->setJson($key, $datas, $timeout);
        }
        return $datas;
    }

    public static function delCache()
    {
        $key = self::getCacheKey();
        RedisService::instance(RedisService::CACHE_DB_BASE)->del($key);
    }

    public static function getProjectIdByVendor($vendor = '')
    {
        $data = self::getByVendor($vendor);
        if ($data) {
            return $data['project_id'];
        } else {
            NotifyDingTalkService::pushTextByAMQP("您正在使用 vendor:" . $vendor . " 获取阿里云物联相关信息 但该vendor未配置 物联项目信息{ProjectInfo}");
        }
        return null;
    }

    public static function addProject($param)
    {
        $item  = Helper::arrayCamelKeyToUnderscore($param);
        self::add($item);
        self::delCache();
    }

    public static function updateProject($param)
    {
        $id =  $param['projectId'];
        unset($param['projectId']);
        $origin = self::getBase($id);
        if (empty($origin)) {
            throw new ApiException("origin data is not exist", ErrnoPlus::COMMON_ERROR);
        }
        $item = Helper::arrayCamelKeyToUnderscore($param);
        $update = [];
        foreach ($item as $key => $val) {
            if ($val != $origin[$key]) {
                $update[$key] = $val;
            }
        }
        if (count($update) > 0) {
            self::update($id, $update);
        }
        // 调用了就删缓存不管有没有修改
        self::delCache();
    }

    public static function delProject($id)
    {
        self::del($id);
        self::delCache();
    }

    public static function buildCondition($filter)
    {
        return [];
    }
}
